package model.entity;

import game_engine.GameEngine;
import game_engine.core.Entity;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import model.world.WorldGenerator;
import resources.ImageHandler;

public class Background extends Entity {
	
	public static final int FRAME_BASE = 0;
	private static int backgroundCount = 0;
	private int[][] images;
	private BufferedImage background;
	private Graphics2D backgroundGraphics;
	
	public Background() {
		super("background " + backgroundCount++);
		images = WorldGenerator.getInstance().getNextImage();
		background = new BufferedImage(GameEngine.resolution.width, GameEngine.resolution.height+20, BufferedImage.TYPE_INT_RGB);
		backgroundGraphics = (Graphics2D) background.getGraphics();
		for(int i = 0 ; i < images.length ; i++) {
			for(int j = 0 ; j < images[i].length ; j++) {
				backgroundGraphics.drawImage(ImageHandler.getInstance().getImageNumbers().get(images[i][j]), i*20, j*20, 20, 20, null);
			}
		}
		currentAction = -1;
	}

	@Override
	public void collided(Entity e) {
		dead = true;
	}

	@Override
	public void doAction(float tick) {
	}
	
	private void redrawBackground() {
		BufferedImage tmp = new BufferedImage(background.getWidth(), background.getHeight(), BufferedImage.TYPE_INT_RGB);
		tmp.getGraphics().drawImage(background, 0, 0, null);
		backgroundGraphics.drawImage(tmp, 
			0, 20, background.getWidth(), background.getHeight(), 
			0, 0 , background.getWidth(), background.getHeight()-20, 
			Color.pink, null);
		for(int i = 0 ; i < images.length ; i++) {
			backgroundGraphics.drawImage(ImageHandler.getInstance().getImageNumbers().get(images[i][0]), i*20, 0, 20, 20, null);
		}
	}

	@Override
	public void render() {
		if (position != null) {
			if(position.y>20){
				images = WorldGenerator.getInstance().getNextImage();
				redrawBackground();
				this.position.y = 0;
			}
//			drawImage(background, 0, Math.round(position.y));
		}
	}
}
